/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pessoas.log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.swing.JOptionPane;
import pessoas.interfaces.ILog;

/**
 *
 * @author Bush
 */
public class LogXml implements ILog {

    private File caminhoLog;

    public LogXml() {
        caminhoLog = new File("src/pessoas/data/logs.xml");
    }
    

    @Override
    public void gravarLog(String log) {
        try {            
            String xml = "<mensagem>\n"+"			"+log+"\n		</mensagem>";
            String antigo = lerXmlLog(caminhoLog);
            FileWriter w = new FileWriter(caminhoLog, false);
            PrintWriter pw = new PrintWriter (w); 
            pw.print(antigo);
            pw.println("		"+xml);
            pw.println("	</log>");
            pw.println("</Logs>");
            pw.close();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }
    private String lerXmlLog(File caminho) throws FileNotFoundException, Exception {
        Scanner scan = new Scanner(caminho);
        String linha = "";

        while (scan.hasNextLine()) {
            linha += scan.nextLine()+"\n";
        }
        linha = linha.replace("</Logs>", "	<log>");
        return linha;

    }


}
